package test

import command.CommandExec
import command.CommandPojo.CommandData

import java.io._
import implicits.Implicits._
import org.apache.commons.lang3.StringUtils
import org.joda.time.DateTime
import tool.Tool
import utils.Utils

import scala.collection.parallel.CollectionConverters._

/** Created by yz on 27/7/2023
  */
trait TestTool {

  val parentDir = new File("E:\\mtd_database\\test")

  def diff = {
    val fromDir = new File("E:\\mtd_database\\data\\mummer")
    val toDir = new File("E:\\mtd_database\\test\\collect_out")
    fromDir.myListFiles.foreach { dir =>
      dir.myListFiles.foreach { file =>
        val outDir = new File(toDir, dir.getName)
        val destFile = new File(outDir, s"${file.getName}")
        val noNeedFileNames = List(
          "alg.txt",
          "final.snp_indel.dis.png",
          "final.snp_indel.dis.svg",
          "identity_list.txt",
          "indel.cds.axt.txt",
          "indel.cdsindel.stat.xls",
          "indel.chr.stat.xls",
          "indel.len.dis.xls",
          "indel.len.png",
          "indel.len.svg",
          "indel.txt",
          "query.miss.txt",
          "run.sh",
          "snp.cds.axt.txt",
          "snp.chr.stat.xls",
          "snp.txt",
          "sv.txt",
          "Target-Query.xoy.png",
          "Target-Query.xoy.svg",
          "target.miss.txt"
        )
        if (
          !destFile.exists() && outDir.exists() && (!noNeedFileNames
            .contains(file.getName)) && List(".gbff", ".gbff.gz").forall(x => !file.getName.endsWith(x))
        ) {
          println(file)
        }
      }
    }

  }

  // 20230905
  def statInDelInterMutant = {
    val startTime = System.currentTimeMillis()
    val outDir = new File("E:\\mtd_database\\data\\mummer")
    val columnNames = List(
      "query_ID",
      "InDel_type",
      "InDel_start",
      "InDel_end",
      "ref",
      "ref_start",
      "ref_end",
      "Base",
      "Fr",
      "gene_dis",
      "pos_start",
      "pos_end",
      "strand",
      "gene_id"
    )
    val keys = List("ref", "ref_start", "ref_end", "Base")
    val removeKeys = List("query_ID", "InDel_start", "InDel_end", "Fr", "InDel_type")
    val files = outDir.myListFiles
      .map { dir =>
        new File(dir, "indel.intergenic.info.xls")
      }
      .filter { file =>
        file.exists()
      }
    var i = 0
    val tIdsMap = files.
      //                  take(1000).
    par
      .threadNum(6)
      .flatMap { file =>
        val ts = file.txtLines
          .lineSeqMap(columnNames)
          .map { map =>
            map -- removeKeys
          }
          .map { map =>
            val k = keys.map(map(_))
            val otherInfo = map.filterNot { case (k, v) => keys.contains(k) }.valueList
            (k.mkString("\t"), (file.getParentFile.getName, otherInfo.mkString("\t")))
          }
        i += 1
        if (i % 50 == 0) {
          println(s"file ${i}", Utils.getTime(startTime))
        }
        ts
      }
      .toList
      .groupMap(x => x._1)(y => y._2)
    println(1, Utils.getTime(startTime))
    val contents = tIdsMap.map { x =>
      val diffContent = x._2
        .groupMap(_._2)(_._1)
        .foldLeft("")((x, y) => {
          if (StringUtils.isBlank(x)) {
            s"${y._1}\t${y._2.mkString("|")}"
          } else {
            val xColumns = x.mySplit("\t")
            val yColumns = y._1.mySplit("\t") :+ y._2.mkString("|")
            val newColumns = xColumns.zip(yColumns).map(t => List(t._1, t._2).mkString(";"))
            newColumns.mkString("\t")
          }
        })
      List(x._1, x._2.size.toString, diffContent).mkString("\t")
    }.toList
    println(2, Utils.getTime(startTime))
    val headers = (keys :+ "mut_sample_num") ::: (columnNames.filterNot(x =>
      keys.contains(x) || removeKeys.contains(x)
    ) :+ "BioSample")
    val lines = headers.mkString("\t") :: contents
    lines.toFile(new File(parentDir, "inter_inDel.txt"))
    println(3, Utils.getTime(startTime))
  }

  // 20230905
  def statInDelCdsMutant = {
    val startTime = System.currentTimeMillis()
    val outDir = new File("E:\\mtd_database\\data\\mummer")
    val keys = List("ref", "ref_start", "ref_end", "Base")
    val removeKeys = List("query_ID", "InDel_start", "InDel_end", "Fr", "InDel_type", "Annotation_type")
    val files = outDir.myListFiles
      .map { dir =>
        new File(dir, "indel.info.xls")
      }
      .filter { file =>
        file.exists()
      }
    var i = 0
    val tIdsMap = files.
      //            take(1000).
    par
      .threadNum(6)
      .flatMap { file =>
        val ts = file.txtLines.lineSeqMap
          .filter { map =>
            map("Annotation_type") == "CDS"
          }
          .map { map =>
            map -- removeKeys
          }
          .map { map =>
            val k = keys.map(map(_))
            val otherInfo = map.filterNot { case (k, v) => keys.contains(k) }.valueList
            (k.mkString("\t"), (file.getParentFile.getName, otherInfo.mkString("\t")))
          }
        i += 1
        if (i % 50 == 0) {
          println(s"file ${i}", Utils.getTime(startTime))
        }
        ts
      }
      .toList
      .groupMap(x => x._1)(y => y._2)
    println(1, Utils.getTime(startTime))
    val contents = tIdsMap.map { x =>
      val diffContent = x._2
        .groupMap(_._2)(_._1)
        .foldLeft("")((x, y) => {
          if (!x.nonBlank) {
            s"${y._1}\t${y._2.mkString("|")}"
          } else {
            val xColumns = x.mySplit("\t")
            val yColumns = y._1.mySplit("\t") :+ y._2.mkString("|")
            val newColumns = xColumns.zip(yColumns).map(t => List(t._1, t._2).mkString(";"))
            newColumns.mkString("\t")
          }
        })
      List(x._1, x._2.size.toString, diffContent).mkString("\t")
    }.toList
    println(2, Utils.getTime(startTime))
    val headers = (keys :+ "mut_sample_num") ::: (files.head.txtLines.head.filterNot(x =>
      keys.contains(x) || removeKeys.contains(x)
    ) :+ "BioSample")
    val lines = headers.mkString("\t") :: contents
    lines.toFile(new File(parentDir, "cds_indel.txt"))
    println(3, Utils.getTime(startTime))
  }

  // 20230905
  def recoverIndelNum = {
    val fileName = "indel.info.xls"
    val outDir = new File("E:\\mtd_database\\data\\mummer")
    val files = outDir.myListFiles
      .filter { dir =>
        new File(dir, fileName).exists()
      }
    var i = 0
    files.par
      .threadNum(6)
      .foreach { dir =>
        val numFile = new File(dir, "indel_num.txt")
        val numMap = numFile.txtLines.map { columns =>
          val k = (columns(0), columns(1), columns(2), columns(3), columns(4), columns(5))
          (k, columns(7))
        }.toMap
        val file = new File(dir, fileName)
        val lines = file.txtLines
        lines.lineSeqMap
          .map { map =>
            val k =
              (map("ref"), map("ref_start"), map("ref_end"), map("query_ID"), map("InDel_start"), map("InDel_end"))
            map.updated("InDel_type", numMap(k))
          }
          .csvLines
          .toTxtFile(file)
        i += 1
        if (i % 50 == 0) {
          println(i, s"${dir.getName} ", "complete!")
        }
      }
  }

  // 20230905
  def recoverIndelInterNum = {
    val fileName = "indel.intergenic.info.xls"
    val outDir = new File("E:\\mtd_database\\data\\mummer")
    val files = outDir.myListFiles
      .filter { dir =>
        new File(dir, fileName).exists()
      }
    var i = 0
    files.par
      .threadNum(6)
      .foreach { dir =>
        val numFile = new File(dir, "indel_num.txt")
        val numMap = numFile.txtLines.map { columns =>
          val k = (columns(0), columns(1), columns(2), columns(3), columns(4), columns(5))
          (k, columns(7))
        }.toMap
        val file = new File(dir, fileName)
        val lines = file.txtLines
        lines
          .map { columns =>
            val k =
              (columns(4), columns(5), columns(6), columns(0), columns(2), columns(3))
            columns.updated(1, numMap(k))
          }
          .toTxtFile(file)
        i += 1
        if (i % 50 == 0) {
          println(i, s"${dir.getName} ", "complete!")
        }
      }
  }

  // 20230905
  def statSnpCdsMutant = {
    val startTime = System.currentTimeMillis()
    val outDir = new File(parentDir, "mummer")
    val keys = List("ref_id", "position", "sample_base", "ref_base")
    val files = outDir.myListFiles.map { dir =>
      new File(dir, "snp.cds.info.xls")
    }
    var i = 0
    val tIdsMap = files.
      //      take(1000).
    par
      .threadNum(6)
      .flatMap { file =>
        val ts = file.txtLines.lineSeqMap.map { map =>
          val k = keys.map { t =>
            map(t)
          }
          val otherInfo = map.filterNot { case (k, v) => keys.contains(k) }.valueList
          (k.mkString("\t"), (file.getParentFile.getName, otherInfo.mkString("\t")))
        }
        i += 1
        val num = 50
        if (i % num == 0) {
          println(s"file ${i}", Utils.getTime(startTime))
        }
        ts
      }
      .toList
      .groupMap(x => x._1)(y => y._2)
    println(1, Utils.getTime(startTime))
    val contents = tIdsMap.map { x =>
      val diffContent = x._2
        .groupMap(_._2)(_._1)
        .foldLeft("")((x, y) => {
          if (x.inBlank) {
            s"${y._1}\t${y._2.mkString("|")}"
          } else {
            val xColumns = x.mySplit("\t")
            val yColumns = y._1.mySplit("\t") :+ y._2.mkString("|")
            val newColumns = xColumns.zip(yColumns).map(t => List(t._1, t._2).mkString(";"))
            newColumns.mkString("\t")
          }
        })
      List(x._1, x._2.size.toString, diffContent).mkString("\t")
    }.toList
    println(2, Utils.getTime(startTime))
    val headers =
      (keys :+ "mut_sample_num") ::: (files.head.txtLines.head.filterNot(x => keys.contains(x)) :+ "BioSample")
    val lines = headers.mkString("\t") :: contents
    lines.toFile(new File(parentDir, "cds_snp.txt"))
    println(3, Utils.getTime(startTime))
  }

  // 20230905
  def snpCdsSplit = {
    val startTime = System.currentTimeMillis()
    val outDir = new File("E:\\mtd_database\\data\\mummer")
    val maps = new File(parentDir, "prokaryotes.csv").csvLines.lineSeqMap
    val files = maps
      .map { map =>
        val id = map("BioSample")
        val dir = new File(outDir, id)
        new File(dir, "snp.cds.info.xls")
      }
      .filter { file =>
        file.exists()
      }
    var i = 0
    files.par.threadNum(6).foreach { file =>
      val newLines = file.txtLines.map { columns =>
        val column3 = columns(2).mySplit("<->")
        columns.take(2) ::: column3 ::: columns.drop(3)
      }
      newLines.toTxtFile(new File(parentDir, s"mummer/${file.getParentFile.getName}/${file.getName}"))
      i += 1
      if (i % 50 == 0) {
        println(s"file $i complete!")
      }
    }
    println(Utils.getTime(startTime))
  }

  // 20230905
  def statSnpInterMutant = {
    val startTime = System.currentTimeMillis()
    val outDir = new File("E:\\mtd_database\\data\\mummer")
    val maps = new File(parentDir, "prokaryotes.csv").csvLines.lineSeqMap
    val keys = List("ref_id", "position", "sample_base")
    val files = maps
      .map { map =>
        val id = map("BioSample")
        val dir = new File(outDir, id)
        new File(dir, "snp.intergenic.info.xls")
      }
      .filter { file =>
        file.exists()
      }
    var i = 0
    val tIdsMap = files.par
      .threadNum(6)
      .flatMap { file =>
        i += 1
        if (i % 50 == 0) {
          println(s"file $i complete!")
        }
        file.txtLines.lineSeqMap.map { map =>
          val k = keys.map(map(_))
          val otherInfo = map.filterNot { case (k, v) => keys.contains(k) }.valueList
          (k, (file.getParentFile.getName, otherInfo))
        }
      }
      .toList
      .groupMap(x => x._1)(y => y._2)
    val contents = tIdsMap.map { x =>
      (x._1 :+ x._2.size.toString) ::: (x._2.head._2 :+ x._2.map(_._1).mkString("|"))
    }.toList
    val headers =
      (keys :+ "mut_sample_num") ::: (files.head.txtLines.head.filterNot(x => keys.contains(x)) :+ "BioSample")
    val lines = headers :: contents
    lines.toTxtFile(new File(parentDir, "inter_snp.txt"))
    println(Utils.getTime(startTime))
  }

  def bioSampleDiff = {
    val newFile = new File(parentDir, "prokaryotes_new.csv")
    val newBioSamples = newFile.csvLines.lineSeqMap.map(_("BioSample"))
    val oldFile = new File(parentDir, "prokaryotes.csv")
    val bioSamples = oldFile.csvLines.lineSeqMap.map(_("BioSample"))
    println(newBioSamples.diff(bioSamples))
  }

  def gunzipGenomic = {
    val outDir = new File(parentDir, "genomic")
    val maps = new File(parentDir, "prokaryotes.csv").csvLines.lineSeqMap
    val startTime = System.currentTimeMillis()
    val dirs = maps.map { map =>
      val id = map("BioSample")
      new File(outDir, id)
    }
    val commandDatas = dirs
      .filter { dir =>
        val gzFileExist = dir.myListFiles.exists(_.getName.endsWith(".fna.gz"))
        val fnaFileExist = dir.myListFiles.exists(_.getName.endsWith("genomic.fna"))
        gzFileExist && (!fnaFileExist)
      }
      .map { dir =>
        val gzFile = dir.myListFiles.find(_.getName.endsWith(".fna.gz")).get
        val command =
          s"""
             |gunzip -k ${gzFile.unixPath}
             |""".stripMargin
        CommandData(dir, command)
      }
      .par
      .threadNum(3)
    println(commandDatas.size)
    val commandExec = CommandExec().parExec { b =>
      commandDatas
    }
    if (commandExec.isSuccess) {
      println(Utils.getTime(startTime))
    } else {
      println(commandExec.errorInfo)
    }

  }

  def kmer = {
    val outDir = new File(parentDir, "genomic")
    val maps = new File(parentDir, "prokaryotes.csv").csvLines.lineSeqMap
    val startTime = System.currentTimeMillis()
    val dirs = maps
      .map { map =>
        val id = map("BioSample")
        new File(outDir, id)
      }
      .filter { dir =>
        val fileExist = dir.myListFiles.exists(_.getName.endsWith(".fna"))
        fileExist
      }
    println(dirs.size)
    val commandDatas = dirs
      .filter { dir =>
        val splitFile = new File(dir, "split_features.csv")
        !splitFile.exists()
      }
      .map { dir =>
        val faFile = dir.myListFiles.find(_.getName.endsWith(".fna")).get
        val command =
          s"""
             |python3 ${Tool.pyPath.unixPath}/newKmer.py --in ${faFile.unixPath} --kmer 8 --out_dir split_features.csv
             |""".stripMargin
        CommandData(dir, command)
      }
      .par
      .threadNum(12)
    println(commandDatas.size)
    val commandExec = CommandExec().parExec { b =>
      commandDatas
    }
    if (commandExec.isSuccess) {
      println(Utils.getTime(startTime))
    } else {
      println(commandExec.errorInfo)
    }

  }

  def mergeSplitFeature = {
    val outDir = new File(parentDir, "genomic")
    val maps = new File(parentDir, "prokaryotes.csv").csvLines.lineSeqMap
    val startTime = System.currentTimeMillis()
    val dirs = maps
      .map { map =>
        val id = map("BioSample")
        new File(outDir, id)
      }
      .filter { dir =>
        val fileExist = new File(dir, "split_features.csv").exists()
        fileExist
      }
      .filter { dir =>
        val file = new File(dir, "features.csv")
        !file.exists()
      }
    println(dirs.size)
    dirs.par.threadNum(12).foreach { dir =>
      val splitFile = new File(dir, "split_features.csv")
      val sampleName = dir.getName
      val lines = splitFile.lines
      val header = lines.head
      val contents = lines.drop(1).reduceLeft { (line1, line2) =>
        val columns1 = line1.mySplit(",")
        val columns2 = line2.mySplit(",")
        columns1
          .zip(columns2)
          .map { case (x, y) =>
            if (x.isInt) {
              s"${x.toInt + y.toInt}"
            } else x
          }
          .mkString(",")
      }
      val finalContents = contents.mySplit(",").init :+ sampleName
      val newLines = List(header, finalContents.mkString(","))
      newLines.toFile(new File(dir, "features.csv"))
      println(sampleName)
    }
    println(Utils.getTime(startTime))
  }

  def mergeFeature = {
    val outDir = new File(parentDir, "genomic")
    val maps = new File(parentDir, "prokaryotes.csv").csvLines.lineSeqMap
    val startTime = System.currentTimeMillis()
    val dirs = maps
      .map { map =>
        val id = map("BioSample")
        new File(outDir, id)
      }
      .filter { dir =>
        val file = new File(dir, "features.csv")
        file.exists()
      }
    println(dirs.size)
    val outFile = new File(parentDir, "features.csv")
    val bw = new BufferedWriter(new FileWriter(outFile), 16384)
    dirs.zipWithIndex.foreach { case (dir, i) =>
      val file = new File(dir, "features.csv")
      val lines = file.lines
      val newLines = if (i == 0) {
        lines
      } else lines.drop(1)
      newLines.foreach { x =>
        bw.write(s"${x}\n")
      }
      if (i % 200 == 0) {
        println(i, dir, Utils.getTime(startTime))
      }
    }
    bw.close()
    println(Utils.getTime(startTime))
  }

  def filterFeature = {
    val startTime = System.currentTimeMillis()
    val featuresFile = new File(parentDir, "features.csv")
    val groupFile = new File(parentDir, "group.csv")
    val samples = groupFile.csvLines
      .filterNot { columns =>
        List(
          "Unknown",
          "missing",
          "not applicable",
          "not collected",
          "N/A",
          "hospital",
          "Osindisweni Hospital - Occ Health, staff clinic",
          "St Margaret's Hospital",
          "St Margaret's TB Hospital",
          "Stanger Hospital",
          "Not applicable",
          "Christ The King Hospital",
          "M3 TB Hospital",
          "Dundee Hospital"
        ).containsIgnoreCase(columns(1))
      }
      .groupMap(x => x(1))(y => y)
      .filter { case (k, vs) => vs.size > 1 }
      .flatMap { case (k, vs) =>
        vs.map(_(0))
      }
      .toList
    val outFile = new File(parentDir, "filter_features.csv")
    val bw = new BufferedWriter(new FileWriter(outFile), 16384)
    featuresFile.lines.zipWithIndex.foreach { case (line, i) =>
      if (i == 0) {
        bw.write(s"${line}\n")
      } else {
        val columns = line.mySplit(",")
        if (columns.last == "SAMN35655919") { println("new exist!") }
        if (samples.contains(columns.last)) {
          bw.write(s"${line}\n")
        }
      }
      if (i % 200 == 0) {
        println(i, Utils.getTime(startTime))
      }
    }
    bw.close()
    println(Utils.getTime(startTime))
  }

  def filterGroup = {
    val startTime = System.currentTimeMillis()
    val filterFeaturesFile = new File(parentDir, "filter_features.csv")
    val groupFile = new File(parentDir, "group.csv")
    val samples = filterFeaturesFile.lines.drop(1).map(_.mySplit(",").last)
    val lineMap = groupFile.csvLines.map { columns =>
      val newColumns = if (List("Hong Kong", "Taiwan").contains(columns(1))) {
        List(columns(0), "China")
      } else columns
      (columns(0), newColumns)
    }.toMap
    val newLines = samples.map { id =>
      lineMap(id)
    }
    newLines.toFile(new File(parentDir, "filter_group.csv"))
    println(Utils.getTime(startTime))
  }

  def createModel = {
    val startTime = System.currentTimeMillis()
    val command =
      s"""
         |${"python3".mambaPath(
          "nb"
        )} newClassifier.py --in filter_features.csv --class filter_group.csv --out_dir nb_out
         |""".stripMargin
    val commandExec = CommandExec().exec { b =>
      CommandData(command = command, workspace = parentDir)
    }
    if (commandExec.isSuccess) {
      println(Utils.getTime(startTime))
    } else {
      println(Utils.getTime(startTime), commandExec.errorInfo)
    }
  }

  def mummer = {
    val outDir = new File("E:\\mtd_database\\data", "mummer")
    val maps = new File(parentDir, "prokaryotes.csv").csvLines.lineSeqMap
    val startTime = System.currentTimeMillis()

    val commandDatas = maps
      .map { map =>
        val id = map("BioSample")
        new File(outDir, id)
      }
//      .filter { dir =>
//        val gbffFileExist = dir.myListFiles.exists(_.getName.endsWith(".gbff"))
//        val coverFileExist = new File(dir, "cover.txt").exists()
//        gbffFileExist &&
//        (!coverFileExist)
//      }
//      .filterNot { dir =>
//        List("SAMEA1523368").contains(dir.getName)
//      }
      .filter { dir =>
        new DateTime(dir.lastModified()).isAfter(new DateTime("2023-08-15"))
      }
      .filterNot { dir =>
        List("SAMEA1523368").contains(dir.getName)
      }
      .filter { dir =>
        val indelSingle = List(
          "indel.intergenic.info.xls"
        )
        indelSingle.exists { name =>
          !new File(dir, name).exists()
        }
      }
      .map { dir =>
        val gbffFile = dir.myListFiles.find(_.getName.endsWith(".gbff")).get
        val targetFile = new File(parentDir, "GCF_000195955.gbff")
        val command =
          s"""
             |java -jar /mnt/e/mummer-assembly-0.1.jar --query ${gbffFile.unixPath} --target ${targetFile.unixPath} --out ${dir.unixPath}
             |""".stripMargin
        CommandData(dir, command)
      }
      .par
      .threadNum(6)
    println(commandDatas.size)
    val commandExec = CommandExec().parExec { b =>
      commandDatas
    }
    if (commandExec.isSuccess) {
      println(Utils.getTime(startTime))
    } else {
      println(commandExec.errorInfo)
    }
    println("===========================")
  }

  def validAllSuccess = {
    val outDir = new File("E:\\mtd_database\\data", "mummer")
    val dirs = outDir.myListFiles
      .filterNot { dir =>
        List("SAMEA1523368").contains(dir.getName)
      }
      .filter { dir =>
        new File(dir, "workspace").exists()
      }
      .par
      .threadNum(12)
      .foreach { dir =>
        val indelSingle = List(
          "indel.intergenic.info.xls"
        )
        val indels = List(
          "indel.cds.stat.xls",
          "indel.info.xls"
        )
        val svs = List(
          "sv.anno.txt",
          "SV_cycle.png",
          "SV_cycle.svg"
        )
        val snps = List(
          "snp.cds.info.xls",
          "snp.cds.stat.xls",
          "snp.intergenic.info.xls"
        )
        val others = List(
          "axt.txt",
          "cover.txt",
          "Target-Query.parallel.png",
          "Target-Query.parallel.svg",
          "indel_num.txt"
        )
        (indelSingle ::: indels ::: snps ::: svs ::: others).foreach { fileName =>
          val file = new File(dir, fileName)
          if (!file.exists() && others.contains(file.getName)) {
            println(file, " 不存在！")
          }
        }
      }

  }

  def produceUploadMummer = {
    val outDir = new File("E:\\mtd_database\\data", "mummer")
    val newDir = new File("E:\\mtd_database\\data", "new_mummer").createDirectoryWhenNoExist
    val dirs = outDir.myListFiles.par
      .threadNum(12)
      .foreach { dir =>
        val inDir = new File(newDir, dir.getName).createDirectoryWhenNoExist
        val indelSingle = List(
          "indel.intergenic.info.xls"
        )
        val indels = List(
          "indel.cds.stat.xls",
          "indel.info.xls",
          "sv.anno.txt"
        )
        val snps = List(
          "snp.cds.info.xls",
          "snp.cds.stat.xls",
          "snp.intergenic.info.xls"
        )
        val others = List(
          "axt.txt",
          "cover.txt",
          "SV_cycle.png",
          "SV_cycle.svg",
          "Target-Query.parallel.png",
          "Target-Query.parallel.svg",
          "indel_num.txt"
        )
        (indelSingle ::: indels ::: snps ::: others).foreach { fileName =>
          val file = new File(dir, fileName)
          val destFile = new File(inDir, fileName)
          if (!destFile.exists() && file.exists()) {
            file.copyTo(destFile)
          }
        }
      }

  }

  def prepareWorkspace = {
    val outDir = new File("E:\\mtd_database\\data", "mummer")
    val newDir = new File("E:\\mtd_database\\data", "mummer_workspace").createDirectoryWhenNoExist
    val dirs = outDir.myListFiles
                     .filter { dir =>
                       new DateTime(dir.lastModified()).isAfter(new DateTime("2023-08-15"))
                     }
                     .par
      .threadNum(12)
      .foreach { dir =>
        val inDir = new File(newDir, dir.getName).createDirectoryWhenNoExist
        val names=List(
          s"${dir.getName}.gff",
          s"${dir.getName}.cds.fa",
          s"${dir.getName}.genome.fa",
        )
        (names).foreach { fileName =>
          val workspaceDir=new File(dir,"workspace")
          val file = new File(workspaceDir, fileName)
          val inWorkspaceDir=new File(inDir,"workspace").createDirectoryWhenNoExist
          val destFile = new File(inWorkspaceDir, fileName)
          if (!destFile.exists() && file.exists()) {
            file.copyTo(destFile)
          }
        }
      }

  }

  def validCover={
    val outDir = new File("E:\\mtd_database\\data", "mummer")
    val coverFiles=outDir.myListFiles.map{dir=>
      new File(dir,"cover.txt")
    }.filter{file=>
     file.exists()
    }.filter{file=>
      file.txtLines.lineSeqMap.head("Target_alligned(bp)").isEmpty
    }
    println(coverFiles.size)
    coverFiles.take(5).foreach(println)

  }

}
